package io;

import java.io.*;

/**
 * 利用包装流完成字符文件拷贝
 */
public class BufferedCopy {

    public static void main(String[] args) {

        /*
            BufferedReader BufferedWriter 是按照字符操作文件
            不要操作二进制文件(声音 视频 图片)，可能造成文件损坏
         */
        String srcFilePath = "D:\\IdeaCode\\a.txt";
        String destFilePath = "D:\\IdeaCode\\javaStudy\\a.txt";

        BufferedReader br = null;
        String line = null;
        BufferedWriter bw = null;

        try {
            br = new BufferedReader(new FileReader(srcFilePath));
            bw = new BufferedWriter(new FileWriter(destFilePath));
            while((line = br.readLine()) != null) {
                bw.write(line);
                //插入换行符(与系统相关)
                bw.newLine();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                //关闭外层流
                if (br != null) {
                    br.close();
                }
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
